package com.xp.basic.heima.nio.netty.simpledemo.worker;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;

public class TestNettyHandleServer {
    public static void main(String[] args) {
        ChannelFuture channelFuture = new ServerBootstrap()
                .group(new NioEventLoopGroup(),new NioEventLoopGroup(2))
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            //入栈
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println(Thread.currentThread().getName() + " 1 " );
                                //这里有两种调用方式
                                //第一种 ctx 中得调用 出栈得时候只会从当前位置向前出栈
//                                ctx.fireChannelRead(msg);
                                //这一种 出栈得时候会从tail 尾指针向前出栈
                                super.channelRead(ctx, msg);
                            }
                        });
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            //入栈
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println(Thread.currentThread().getName() + " 2 " );
                                //这里有两种调用方式
                                //第一种 ctx 中得调用 出栈得时候只会从当前位置向前出栈
//                                ctx.fireChannelRead(msg);
                                //这一种 出栈得时候会从tail 尾指针向前出栈
                                super.channelRead(ctx, msg);
                            }
                        });
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            //入栈
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println(Thread.currentThread().getName() + " 3 " );
                                //这里有两种调用方式
                                //第一种 ctx 中得调用 出栈得时候只会从当前位置向前出栈
//                                ctx.fireChannelRead(msg);
                                //这一种 出栈得时候会从tail 尾指针向前出栈
                                //在流水线最后一个处理时，可以不调用这个方法
//                                super.channelRead(ctx, msg);
                            }
                        });
                    }
                })
                .bind(8080);
        try {
            // 同步调用
            channelFuture.channel().closeFuture().sync();
            //异步条件监听调用
            channelFuture.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture channelFuture) throws Exception {
                    System.out.println("完成时异步调用。。。");
                }
            });
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
